package cn.edu.scau.cmi.guihao.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.servlet.tags.form.TextareaTag;

import comprehension.cn.edu.scau.cmi.guihao.comprehension.dao.StudentDAO;
import comprehension.cn.edu.scau.cmi.guihao.comprehension.dao.TeacherDAO;
import comprehension.cn.edu.scau.cmi.guihao.comprehension.domain.Student;
import comprehension.cn.edu.scau.cmi.guihao.comprehension.domain.Teacher;
import comprehension.cn.edu.scau.cmi.guihao.comprehension.util.ComprehensionApplicationContextUtil;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

public class HibernateController {

	@FXML
	private TextField addStudentName;
	@FXML
	private TextField updateStudentName;
	@FXML
	private TextField deleteStudentName;
	@FXML
	private TextField findStudentName;
	
	@FXML
	private ComboBox<String> addStudentTutor;
	@FXML
	private ComboBox<String> updateStudentTutor;
	
	@FXML
	private TextArea displayConsole;
	
	private List<Teacher> teachers;
	private List<Student> students;
	
	private StudentDAO studentDAO;
	private TeacherDAO teacherDAO;
	
	public void init(){
		ApplicationContext applicationContext = ComprehensionApplicationContextUtil.getApplicationContext();
		studentDAO=(StudentDAO) applicationContext.getBean("studentDAO");
		teacherDAO=(TeacherDAO) applicationContext.getBean("teacherDAO");
		
		students = studentDAO.findAll();
		teachers = teacherDAO.findAll();
		
		ObservableList<String> teachersName = FXCollections.observableArrayList();
		for(Teacher teacher:teachers) {
			teachersName.add(teacher.getName());
		}
		addStudentTutor.setItems(teachersName);
		updateStudentTutor.setItems(teachersName);
	}
	
	public void addStudent() {
		String studentName = addStudentName.getText();
		String teacherName = addStudentTutor.getValue();
		Teacher tutorTeacher = null;
		for(Teacher teacher:teachers) {
			if(teacher.getName().equals(teacherName)) {
				tutorTeacher = teacher;
			}
		}
		Student student = new Student();
		student.setName(studentName);
		student.setTeacher(tutorTeacher);
		if(studentDAO.save(student)) {
			displayConsole.appendText("\n创建成功");
		}
		students = studentDAO.findAll();
	}
	
	public void updateStudent() {
		String studentName = updateStudentName.getText();
		String teacherName = updateStudentTutor.getValue();
		Teacher tutorTeacher = null;
		for(Teacher teacher:teachers) {
			if(teacher.getName().equals(teacherName)) {
				tutorTeacher = teacher;
			}
		}
		Student updateStudent=null;
		for(Student student:students) {
			if(student.getName().equals(studentName)) {
				updateStudent = student;
			}
		}
		if(updateStudent!=null) {
			updateStudent.setTeacher(tutorTeacher);
			if(studentDAO.update(updateStudent)) {
				displayConsole.appendText("\n修改成功\n学生："+updateStudent.getName()+"  导师："+updateStudent.getTeacher().getName());
			}	
			students = studentDAO.findAll();
		}else {
			displayConsole.appendText("\n查无此学生");
		}
	}
	
	public void findStudent() {
		String studentName = findStudentName.getText();
		System.out.println(studentName);
		Student findStudent=null;
		for(Student student:students) {
			if(student.getName().equals(studentName)) {
				findStudent = student;
			}
		}
		if(findStudent!=null) {
			displayConsole.appendText("\n查找成功\n学生："+findStudent.getName()+"  导师："+findStudent.getTeacher().getName());
		}else {
			displayConsole.appendText("\n查无此学生");
		}
	}
	
	public void deleteStudent() {
		String studentName = deleteStudentName.getText();
		Student deleteStudent=null;
		for(Student student:students) {
			if(student.getName().equals(studentName)) {
				deleteStudent = student;
			}
		}
		if(deleteStudent!=null) {
			if(studentDAO.delete(deleteStudent)) {
				displayConsole.appendText("\n删除成功\n学生："+deleteStudent.getName()+"  导师："+deleteStudent.getTeacher().getName());				
			}
		}else {
			displayConsole.appendText("\n查无此学生");
		}
		students = studentDAO.findAll();
	}
	
	
}
